package com.panda.utils;


import com.panda.pojo.Context;
import com.panda.pojo.Document;
import com.panda.pojo.Element;
import com.panda.pojo.Scope;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class DynamicElementUtils {


    /*
        动态元素使用  new [A-Z][a-z]* 的格式创建

        找到之后向下数行提取body

     */

    private static final Pattern pattern = Pattern.compile("new ([A-Z][a-zA-Z]*)");

    private static final Pattern placeholder = Pattern.compile("\\{[a-zA-Z_][a-zA-Z0-9_]*}");

    private static final Pattern stringP = Pattern.compile("__string_[a-zA-Z][a-zA-Z0-9]*_[0-9]+");

    static String transform2element(Context context, Element el, String s) {


        if ("".equals(s)) {
            return s;
        }

        Set<String> protectedStrings = el.getProtectedStrings();

        Matcher matcher = pattern.matcher(s);

        String r = s;

        char[] chars = s.toCharArray();

        while (matcher.find()) {


            String m = matcher.group();

            //被声明的元素名称
            String statement = matcher.group(1);

            int end = Finder.findBody(chars, matcher.end());

            String element = statement;
            if (end != -1) {
                element = s.substring(matcher.start(), end);
            } else {
                end = Finder.findParam(chars, matcher.end());
                if (end != -1) {
                    element = s.substring(matcher.start(), end);
                }
            }


            //将该元素解析成html的格式

            //剔除掉$scope前缀
            String ne = element.replace(m, statement).replace("$scope.", "");

            Context nContext = new Context(context, ne);

            nContext.setControllerScopes(new HashSet<>());

            ParseUtils.parse(nContext);

            ProcessUtils.process(nContext);

            //去掉换行符
            String net = nContext.toString().replace("\n", " ");

            Matcher h = stringP.matcher(net);

            while (h.find()) {

                String group = h.group();

                //获取字符串
                String k = context.getStringMap().get(group);

                Matcher t = placeholder.matcher(k);

                while (t.find()) {

                    String g = t.group();

                    String ts = "'+" + g.substring(1, g.length() - 1) + "+'";

                    //替换
                    context.getStringMap().put(group, k.replace(g, ts));

                }


            }


            if (nContext.getParsingComponent() == null) {

                nContext.getRootElement().handleDep();

                Set<Scope> controllerScopes = nContext.getControllerScopes();

                context.getControllerScopes().addAll(controllerScopes);
            }

            StringBuilder js = new StringBuilder();

/*

            for (Scope scope : controllerScopes) {

                String scopeScript = scope.getScript();

                js.append("\n");

                Set<String> services = scope.getDependencies();

                js.append("\nmodule.register.controller('").append(scope.getId()).append("', function (").append(Joiner.on(",").join(services)).append(") {\n\n").append(scopeScript).append("\n\n});");

            }

*/

            //将js使用字符串符号隐藏

            Map<String, String> stringMap = context.getStringMap();

            String jsId = IdUtils.create("__js_string_");

            //在这里动态注册一个controller
            /*
                module..controller("", function ($scope) {
            });

             */


            //其中的占位符表达式是被字符串保护着的
            js.append(" $compile($('").append(net).append("'))($scope)");

            stringMap.put(jsId, js.toString());

            protectedStrings.add(jsId);

            //使用jsId将js隐藏
            r = r.replace(element, jsId);

        }

        return r;

    }

}
